import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syria_sports/data/controllers/subscriptions_controller.dart';
import 'package:syria_sports/presentation/dashboard_container_screen/dashboard_container_screen.dart';

import '../../../core/utils/show_toast.dart';
import '../../../data/controllers/offers_controller.dart';
import '../models/offer.dart';

part 'offers_state.dart';

class OffersCubit extends Cubit<OffersState> {
  OffersCubit() : super(OffersInitial());

  static OffersCubit get(context) => BlocProvider.of(context);

  List <Offer> offers = [];
  void init() {
    getAllOffers();
  }

  Future getAllOffers()async{
    offers = [];
    emit(OffersLoading());
    await OffersController.getAllOffers().catchError((e){
      emit(OffersError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        offers = List<Offer>.generate(value.data.length, (index) => Offer.fromJson(value.data[index]));
        offers= offers.reversed.toList();
        emit(OffersDone());
      }else if(value != null){
        showMessage(value.message);
        emit(OffersError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }

  selectOffer(Offer offer){
    SubscriptionsController.addAdminSubscription().then((value) {
      if(value!= null && value.state == true){
        print('tet');
        emit(OffersSelectDone());
      }
      print(value);

    });
  }

}
